<?php 

	include 'includes/conectionMySql.inc';
	// Variável de Conexão: $db;
	
	$idFuncionario = $_GET['id'];
	$data = date("Y-m-d");
	$query = "SELECT status FROM funcionarios func where id =".$idFuncionario;
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	if($row[0]==4){
		header ("Location: dummy.php?x=2");
		exit();
	}
	$query = "UPDATE funcionarios SET status = 4, data_demissao =  CURRENT_TIMESTAMP WHERE id = $idFuncionario";
	mysql_query($query) or die('Erro Ao executar query. '.mysql_error($db));
	$query = "UPDATE contratados SET demitido = 1, dataDemissao = '$data' where id_funcionario=".$idFuncionario;
	mysql_query($query) or die("Erro ao executar query. ".mysql_error($result));
	$idVaga = 0;
	$idCurriculo = 0;
	$query = "select count(*) as count from contratados where id_funcionario =".$idFuncionario;
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	if($row[0]>0){
		$query = 'SELECT id_vaga, id_pessoa FROM contratados WHERE id_funcionario = '.$idFuncionario;
		$result = mysql_query($query);
		$row = mysql_fetch_array($result);
		$idVaga = $row[0];
		$idCurriculo = $row[1];
	}
	echo $idVaga;
	echo $idCurriculo;
		
	$query = 'INSERT INTO topoffice_demissao_temp (id_curriculo,id_funcionario,id_vaga,data_demissao) VALUES ('.$idCurriculo.','.$idFuncionario.','.$idVaga.',\''.$data.'\')';
	
	mysql_query($query);
	
	if(mysql_affected_rows($db)>0){
		$tipoMensagem = 4;
		include 'testeEmail.php';	
	}else{
		mysql_close($db);
	}
?>